package com.second.app.future;

import java.util.concurrent.*;

/**
 * @Author GYZ
 * @Date 2024/2/1
 * <pre>
 *   callable和future
 * </pre>
 */
public class CallableTask {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2,
                5, 10,
                TimeUnit.SECONDS, new LinkedBlockingDeque<>());

        Callable<String> callable = () -> "返回一个字符串";
        Future<String> result = executor.submit(callable);
        //get 具有阻塞性
        System.out.println(result.get());
        System.out.println("end-----");
    }
}
//使用ExecutorService接口中的方法submit(Runnable,T result)

class UserInfo{
    private String userName;
    private String password;

    public UserInfo(){
        super();
    }

    public UserInfo(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
class MyRunnable implements Runnable{

    private UserInfo userInfo;

    public MyRunnable(UserInfo userInfo){
        this.userInfo =userInfo;
    }
    @Override
    public void run() {
        userInfo.setPassword("passwordValue");
        userInfo.setUserName("usernameValue");
    }
}

class Main{
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        UserInfo userInfo = new UserInfo();
        MyRunnable myRunnable = new MyRunnable(userInfo);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10,
                10, 10,
                TimeUnit.SECONDS, new LinkedBlockingDeque<>());
        Future<UserInfo> future = executor.submit(myRunnable, userInfo);
        System.out.println("begin time="+System.currentTimeMillis());
        userInfo=future.get();
        System.out.println("get value "+userInfo.getUserName()+" "+userInfo.getPassword());
        System.out.println("end time="+System.currentTimeMillis());
    }
}
